package at.fhj.itm09.snake.activities;

import android.app.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import at.fhj.itm09.snake.R;

/**
 * 
 * @author Susanne Brandstätter - Susanne.Brandstaetter.ITM09@fh-joanneum.at
 * @author Katharina Biela - Katharina.Biela.ITM09@fh-joanneum.at
 *
 * This activity gets called by clicking on the button in the main activity.
 * 
 * It contains the settings where you can choose which color the head of the snake
 * should have and which background you will use during playing.
 * It also contains 2 other buttons where you can make a photo or 
 * load a photo by url from the net.
 * 
 */

public class SettingsActivity extends Activity implements OnClickListener{

	private RadioButton rbGreen;
	private RadioButton rbRed;
	private RadioButton rbYellow;
	private RadioButton rbMyPhoto;
	private RadioButton rbBlack;
	private RadioButton rbLoadPhoto;
	private Button saveSettings;
	private Button makePhoto;
	private Button loadPhoto;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.settings);
        
        rbGreen = (RadioButton) findViewById(R.id.radioGreen);
        rbRed = (RadioButton) findViewById(R.id.radioRed);
        rbYellow = (RadioButton) findViewById(R.id.radioYellow);
        rbMyPhoto = (RadioButton) findViewById(R.id.radioMyPhoto);
        rbLoadPhoto = (RadioButton) findViewById(R.id.radioLoadPhoto);
        rbBlack = (RadioButton) findViewById(R.id.radioBlack);
        saveSettings = (Button) findViewById(R.id.saveSettings);
        makePhoto = (Button) findViewById(R.id.makePhoto);
        loadPhoto = (Button) findViewById(R.id.urlPhoto);
        
        
        /*
         * It loads all preferences
         */
		SharedPreferences settingPrefs = getSharedPreferences("mySharedSettings", 0);
		rbGreen.setChecked(settingPrefs.getBoolean("rbXML", false));
		rbRed.setChecked(settingPrefs.getBoolean("rbRed", false));
		rbYellow.setChecked(settingPrefs.getBoolean("rbYellow", false));
		rbMyPhoto.setChecked(settingPrefs.getBoolean("rbMyPhoto", false));
		rbLoadPhoto.setChecked(settingPrefs.getBoolean("rbLoadPhoto", false));
		rbBlack.setChecked(settingPrefs.getBoolean("rbBlack", false));
		
		
		
		rbGreen.setOnClickListener(this);
		rbRed.setOnClickListener(this);
		rbYellow.setOnClickListener(this);
		rbMyPhoto.setOnClickListener(this);
		rbBlack.setOnClickListener(this);
		rbLoadPhoto.setOnClickListener(this);
		
		saveSettings.setOnClickListener(this);
		makePhoto.setOnClickListener(this);
		loadPhoto.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		SharedPreferences settingPrefs = getSharedPreferences("mySharedSettings", 0);
		SharedPreferences.Editor prefsEdit = settingPrefs.edit(); 
				
		if(rbGreen.isChecked()){
			rbGreen.setTypeface(null, Typeface.BOLD);
			rbRed.setTypeface(null, Typeface.NORMAL);
			rbYellow.setTypeface(null, Typeface.NORMAL);
		}
		if(rbRed.isChecked()){
			rbRed.setTypeface(null, Typeface.BOLD);
			rbGreen.setTypeface(null, Typeface.NORMAL);
			rbYellow.setTypeface(null, Typeface.NORMAL);
		}
		if(rbYellow.isChecked()){
			rbYellow.setTypeface(null, Typeface.BOLD);
			rbRed.setTypeface(null, Typeface.NORMAL);
			rbGreen.setTypeface(null, Typeface.NORMAL);
		}

		
		/*
		 * If the button "Einstellungen gespeichert" is pressed, it will save all
		 * preferences to can use them afterwards. 
		 */
		if(saveSettings.isPressed()){
			prefsEdit.putBoolean("rbGreen", rbGreen.isChecked());
			prefsEdit.putBoolean("rbRed", rbRed.isChecked());
			prefsEdit.putBoolean("rbYellow", rbYellow.isChecked());
			prefsEdit.putBoolean("rbMyPhoto", rbMyPhoto.isChecked());
			prefsEdit.putBoolean("rbBlack", rbBlack.isChecked());
			prefsEdit.putBoolean("rbLoadPhoto", rbLoadPhoto.isChecked());
			prefsEdit.commit();
			
			Toast t = Toast.makeText(this, "Wurde gespeichert!", Toast.LENGTH_LONG);
			t.show();
			this.finish();
		}
		
		/*
		 * If you click on the button "Foto machen" it will start the camera activity.
		 */
		if(makePhoto.isPressed()){
			Intent photoIntent = new Intent(this, CameraActivity.class);
			startActivity(photoIntent);

			rbMyPhoto.setChecked(true);
		}
		
		/*
		 * If you click on the button "Foto laden" it will start the load picture activity.
		 */
		if(loadPhoto.isPressed()){
			Intent loadIntent = new Intent(this, LoadPictureActivity.class);
			startActivity(loadIntent);
			
			rbLoadPhoto.setChecked(true);
		}
	}
}
